package com.qc.dao.impl;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qc.common.StringUtil;
import com.qc.dao.AuthenticationDao;
import com.qc.entity.Authentication;
import com.qc.entity.UserBossEntity;
import com.qc.entity.UserSecretaryEntity;

@SuppressWarnings({ "unchecked", "rawtypes" })
@Repository("authenticationDao")
public class AuthenticationDaoImpl implements AuthenticationDao{

	@Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;
	
	@Override
	public int saveAuthentication(Authentication authentication) {
		
		String sql = "insert into t_authentication(user_id,upload_image,company,post,type,describes) values(?,?,?,?,?,?)";
		return jdbcTemplate.update(sql,new Object[]{
					authentication.getUserId(),authentication.getUploadImage(),
					authentication.getCompany(),authentication.getPost(),
					authentication.getType(),authentication.getDescribes()});
	}

	@Override
	public int deleteAuthenticationId(long id) {

		return 0;
	}

	@Override
	public int updateAuthenticationId(Authentication authentication) {
		
		String sql = "update t_authentication set type = ?,upload_image = ? ,describes = ? where t_id = ? ";
		
        return jdbcTemplate.update(sql,new Object[]{authentication.getType(),authentication.getUploadImage(),authentication.getDescribes(),authentication.gettId()});
		
	}

	@Override
	public Authentication getAuthentication(String userId) {

		String sql = "select * from t_authentication where 1 = 1 and if_del = '0' ";
        
        if (StringUtil.isNotEmpty(userId) && !"undefined".equals(userId) ) {
            sql += " and user_id = '"+userId+"' ";
        }
        Authentication authentication = new Authentication(); 
        try {
			authentication = (Authentication) jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(Authentication.class));
		} catch (DataAccessException e) {
			
		}
        return authentication;
	}

	@Override
	public UserSecretaryEntity getSecretaryId(String userId) {
		String sql = "select job,school from t_user_secretary where 1 = 1 and if_del = '0'";
		if (StringUtil.isNotEmpty(userId) && !"undefined".equals(userId) ) {
            sql += " and user_id = '"+userId+"' ";
        }
		return (UserSecretaryEntity) jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(UserSecretaryEntity.class));
	}

	@Override
	public UserBossEntity getUserBossId(String userId) {
		
		String sql = "select company,duty from t_user_boss where 1 = 1 and if_del = '0'";
		if (StringUtil.isNotEmpty(userId) && !"undefined".equals(userId) ) {
            sql += " and user_id = '"+userId+"' ";
        }
		return (UserBossEntity) jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(UserBossEntity.class));
	}

}
